package cn.zhang.reimbursement_1.service.impl;

import cn.zhang.reimbursement_1.common.CommonMethod;
import cn.zhang.reimbursement_1.config.LoginInterceptor;
import cn.zhang.reimbursement_1.mapper.RecordMapper;
import cn.zhang.reimbursement_1.model.Record;
import cn.zhang.reimbursement_1.request.LoginRequest;
import cn.zhang.reimbursement_1.tools.JsonData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhang.reimbursement_1.model.Type;
import cn.zhang.reimbursement_1.service.TypeService;
import cn.zhang.reimbursement_1.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author zfs90
* @description 针对表【t_type】的数据库操作Service实现
* @createDate 2022-03-25 14:52:48
*/
@Service
public class TypeServiceImpl implements TypeService{

    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private RecordMapper recordMapper;


    /**
     * 添加报销类型
     * @param type
     * @return
     */
    @Override
    public JsonData saveType(Type type) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        if (type.getType()==null){
            return JsonData.buildCodeAndMsg(-1,"添加对象不可为空");
        }
        if (CommonMethod.checkRole(loginRequest)){
            typeMapper.insert(type);
            return JsonData.buildSuccess();
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");
    }

    /**
     * 查询报销类型
     * @return
     */
    @Override
    public JsonData getType() {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
            List<Type> types = typeMapper.selectList(new QueryWrapper<Type>());
            return JsonData.buildSuccess(types);

    }

    /**
     * 修改报销类型
     * @param type
     * @return
     */
    @Override
    public JsonData updateType(Type type) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        Record record = new Record();

        if (CommonMethod.checkRole(loginRequest)){
            typeMapper.updateById(type);

            record.setRealname(loginRequest.getUsername());
            record.setOperation("修改");
            record.setInsertTime(new Date());
            record.setDetails("修改了"+type.getType());
            recordMapper.insert(record);

            return JsonData.buildSuccess();

        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");
    }

    /**
     * 删除报销类型
     * @param delId
     * @return
     */
    @Override
    public JsonData delType(Integer delId) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        Record record = new Record();
        Type type = typeMapper.selectById(delId);
        if (CommonMethod.checkRole(loginRequest)){
            typeMapper.deleteById(delId);

            record.setRealname(loginRequest.getUsername());
            record.setOperation("删除");
            record.setInsertTime(new Date());
            record.setDetails("删除了"+type.getType());
            recordMapper.insert(record);
            return JsonData.buildSuccess();
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");

    }
}




